const app = getApp();
var api = require("../../api/api.js");
var utils = require("../../utils/utils.js");
var event = require("../../event/event.js");
var storageManage = require("../../manage/storageManage.js");
var wxParse = require('../../wxParse/wxParse.js');
var apiRequest = require("../../api/wxRequest.js");
Page({
  data: {
    // 轮播图
    images: [],
    commodity_name: '',
    price: 0,
    isScroll: false,
    taptype:''
  },
  onShow: function () {

  },

  onHide: function () {
  },

  onLoad: function (options) {
    wx.showLoading()
    var that = this;
    api.default.check(function (res) {
      that.data.mCommodityId = options.commodity_id;
      api.default.api('get_commodity_detail', { "commodity_id": that.data.mCommodityId }, that.successCallback, that.errorCallback);
      api.default.api('get_commodity_detail_richtext', { "commodity_id": that.data.mCommodityId }, that.richtextSuccessCallback, that.richtextErrorCallback);
    })




  },

  onReady: function () {
    //获得modalContainer组件
    this.modalContainer = this.selectComponent("#modalContainer");
  },
  successCallback: function (data) {
    var that = this;
    this.setData({
      data: data.data,
      commodity_name: data.data.commodity_name,
      price: data.data.price,
      uid: data.data.uid
    });
    wx.hideLoading();

  },

  errorCallback: function (e, ed) {

  },
  //获取详情文本
  richtextSuccessCallback: function (data) {
    wxParse.wxParse('article', 'html', data.data.nodes, this, 5);
  },
  richtextErrorCallback: function (e) {
    console.log(e)
  },

  //选择规格
  showModal: function (e) {
    if (e.currentTarget.dataset.taptype =='buy'){
      this.setData({
        taptype: "buy"
      })
    }else{
      this.setData({
        taptype: "addcart"
      })
    }
    console.log(this.data.taptype)
    this.modalContainer.showModalContainer(this.data);
  },
  /**
   * 跳转购物车
   */
  cartListFun: function () {
    wx.switchTab({
      url: '../shoppingcart/shoppingcart',
    });
  },
  //组件传递的方法
  hideModal: function () {
    console.log("组件被关闭")
    this.setData({
      isScroll: false
    })
  },

  previewImage: function () {
    wx.previewImage({
      urls: this.data.codeImg.split(',')
    })
  }
})